#!/usr/bin/env/ node
//Node Cli 入口文件
console.log('cli working')

//1.通过命令行交互询问用户问题
//2.根据用户回答的结果生成文件

const inquirer = require('inquirer')
const fs = require('fs')
const path = require('path')
const ejs = require('ejs')

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'Project name?'
    }
])
.then(answer => {
    console.log(answer)
    //根据用户回答的结果生成文件
    const templDir = path.join(__dirname, 'templates')
    //目标目录
    const destDir = process.cwd()

    // 将模版下的文件全部转换到目标目录
    fs.readdir(templDir, (err, files) => {
         if(err) throw err
         files.forEach(file => {
             ejs.renderFile(path.join(templDir, file),  answer, (err, result) => {
                if(err) throw err
                fs.writeFileSync(path.join(destDir, file), result)
             })
         });
    })
})